package com.zhss.eshop.wms.stock;


import com.zhss.eshop.order.domain.dto.OrderItemDTO;
import com.zhss.eshop.schedule.domain.SaleDeliveryScheduleResult;
import com.zhss.eshop.schedule.domain.dto.ScheduleOrderPickingItemDTO;
import com.zhss.eshop.schedule.domain.dto.ScheduleOrderSendOutDetailDTO;
import com.zhss.eshop.wms.domain.model.WmsGoodsAllocationStock;
import com.zhss.eshop.wms.domain.model.WmsGoodsAllocationStockDetail;
import com.zhss.eshop.wms.domain.model.WmsGoodsStock;
import com.zhss.eshop.wms.mapper.WmsGoodsAllocationStockDetailMapper;
import com.zhss.eshop.wms.mapper.WmsGoodsAllocationStockMapper;
import com.zhss.eshop.wms.mapper.WmsGoodsStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 提交订单库存更新组件
 * @author suyuan
 *
 */
@Component
@Scope("prototype")
public class SubmitOrderWmsStockUpdater extends AbstractWmsStockUpdater {

	/**
	 * 调度结果
	 */
	private SaleDeliveryScheduleResult scheduleResult;

	/**
	 * 商品库存管理的DAO组件
	 */
	@Autowired
	private WmsGoodsStockMapper goodsStockDAO;
	/**
	 * 货位库存管理的DAO组件
	 */
	@Autowired
	private WmsGoodsAllocationStockMapper goodsAllocationStockDAO;
	/**
	 * 货位库存明细管理的DAO组件
	 */
	@Autowired
	private WmsGoodsAllocationStockDetailMapper stockDetailDAO;

	/**
	 * 更新商品库存
	 */
	@Override
	protected void updateGoodsStock() throws Exception {
		OrderItemDTO orderItem = scheduleResult.getOrderItem();

		WmsGoodsStock goodsStock = goodsStockDAO.getBySkuId(orderItem.getGoodsSkuId());

		Long availableStockQuantity = goodsStock.getAvailableStockQuantity()
				- orderItem.getPurchaseQuantity();
		goodsStock.setAvailableStockQuantity(availableStockQuantity);

		Long lockedStockQuantity = goodsStock.getLockedStockQuantity()
				+ orderItem.getPurchaseQuantity();
		goodsStock.setLockedStockQuantity(lockedStockQuantity);

		goodsStockDAO.updateByPrimaryKeySelective(goodsStock);
	}

	/**
	 * 更新货位库存
	 */
	@Override
	protected void updateGoodsAllocationStock() throws Exception {
		List<ScheduleOrderPickingItemDTO> pickingItems = scheduleResult.getPickingItems();

		for(ScheduleOrderPickingItemDTO pickingItem : pickingItems) {
			WmsGoodsAllocationStock goodsAllocationStock = goodsAllocationStockDAO
					.getBySkuId(pickingItem.getGoodsAllocationId(), pickingItem.getGoodsSkuId());

			Long availableStockQuantity = goodsAllocationStock.getAvailableStockQuantity()
					- pickingItem.getPickingCount();
			goodsAllocationStock.setAvailableStockQuantity(availableStockQuantity);

			Long lockedStockQuantity = goodsAllocationStock.getLockedStockQuantity()
					+ pickingItem.getPickingCount();
			goodsAllocationStock.setLockedStockQuantity(lockedStockQuantity);

			goodsAllocationStockDAO.updateByPrimaryKeySelective(goodsAllocationStock);
		}
	}

	/**
	 * 更新货位库存明细
	 */
	@Override
	protected void updateGoodsAllocationStockDetail() throws Exception {
		List<ScheduleOrderSendOutDetailDTO> sendOutDetails = scheduleResult.getSendOutDetails();

		for(ScheduleOrderSendOutDetailDTO sendOutDetail : sendOutDetails) {
			WmsGoodsAllocationStockDetail stockDetail = stockDetailDAO.selectByPrimaryKey(
					sendOutDetail.getGoodsAllocationStockDetailId());

			Long currentStockQuantity = stockDetail.getCurrentStockQuantity()
					- sendOutDetail.getSendOutCount();
			stockDetail.setCurrentStockQuantity(currentStockQuantity);

			Long lockedStockQuantity = stockDetail.getLockedStockQuantity()
					+ sendOutDetail.getSendOutCount();
			stockDetail.setLockedStockQuantity(lockedStockQuantity);

			stockDetailDAO.updateByPrimaryKeySelective(stockDetail);
		}
 	}

	/**
	 * 设置需要的参数
	 */
	@Override
	public void setParameter(Object parameter) {
		this.scheduleResult = (SaleDeliveryScheduleResult) parameter;
	}

}
